package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/7/5 10:48
 * @Version 1.0
 */
@Api(tags = "商品后台管理模块")
@RestController // @ResponseBody+@Controller
@RequestMapping("/admin/product/")
//@CrossOrigin
public class ManageController {

    @Autowired
    private ManageService manageService;

    /**
     * 获取所有一级分类数据
     * @return
     */
    @GetMapping("getCategory1")
    public Result getCategory1(){
        //  调用服务层方法
        List<BaseCategory1> baseCategory1List = this.manageService.getCategory1();
        //  返回数据
        return Result.ok(baseCategory1List);
    }

    /**
     * 根据一级分类Id 获取二级分类数据
     * @return
     */
    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){
        //  调用服务此方法
        List<BaseCategory2> baseCategory2List = manageService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }

    /**
     * 根据二级分类Id 查询三级分类数据
     * @param category2Id
     * @return
     */
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){
        //  调用服务此方法
        List<BaseCategory3> baseCategory3List = manageService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }

    /**
     * 根据分类Id 查看平台属性集合
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable Long category1Id,
                               @PathVariable Long category2Id,
                               @PathVariable Long category3Id){
        //  调用服务层方法
        List<BaseAttrInfo> baseAttrInfoList = this.manageService.getAttrInfoList(category1Id,category2Id,category3Id);
        return Result.ok(baseAttrInfoList);
    }

    /**
     * 保存平台属性
     * @param baseAttrInfo
     * @return
     */
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        //  调用服务层方法
        this.manageService.saveAttrInfo(baseAttrInfo);
        //  默认返回ok
        return Result.ok();
    }

    /**
     * 根据平台属性Id 获取到平台属性值集合
     * @param attrId
     * @return
     */
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        //  调用服务层方法
        //        List<BaseAttrValue> baseAttrValueList = this.mangeService.getAttrValueList(attrId);
        //        return Result.ok(baseAttrValueList);
        //  根据平台属性Id 查询平台属性对象
        BaseAttrInfo baseAttrInfo = this.manageService.getBaseAttrInfo(attrId);
        if (null == baseAttrInfo){
            return Result.fail();
        }
        //  返回平台属性值集合
        return Result.ok(baseAttrInfo.getAttrValueList());
    }
}
